import java.util.ArrayList;
import java.util.List;

public class Solution216 {
    public List<List<Integer>> combinationSum3(int k, int n) {
        List<List<Integer>> ans=new ArrayList<>();
        List<Integer> temp=new ArrayList<>();
        dfs(0,ans,temp,1,k,0,n);
        return ans;
    }

    private void dfs(int t, List<List<Integer>> ans, List<Integer> temp, int last,int k,int sum,int n) {
        if (t==k){
            if (sum==n) {
                ans.add(new ArrayList<>(temp));
            }
            return;
        }
        for (int i=last;i<=9;i++){
            temp.add(i);
            dfs(t+1,ans,temp,i+1,k,sum+i,n);
            temp.remove(temp.size()-1);
        }
    }

    public static void main(String[] args) {
        System.out.println(new Solution216().combinationSum3(3,7));
    }
}
